<template>
  <div class="xlsx-btns">
    <el-upload
        ref="upload"
        v-if="status"
        action=""
        :disabled="loading"
        :show-file-list="false"
        :on-change="readExcel"
        :auto-upload="false">
      <el-button
          :disabled="loading"
          slot="trigger"
          icon="el-icon-bottom-right"
          size="mini"
          type="default">
        导入
      </el-button>
    </el-upload>
  </div>
</template>
<script>
import XLSX from 'xlsx'
export default {
  name: 'xlsxImport',
  props: ['status'],
  data () {
    return {
      ABC: [],
      loading: false,
      dataSheets: []
    }
  },
  created () {
    this.init()
  },
  methods: {
    readExcel (file) {
      let that = this
      const fileReader = new FileReader()
      fileReader.onload = (ev) => {
        try {
          const data = ev.target.result
          const workbook = XLSX.read(data, {
            type: 'binary'
          })
          for (let sheet in workbook.Sheets) {
            let tmp = that.parseXLSX(workbook.Sheets[sheet])
            if (tmp) {
              tmp.name = sheet
              that.dataSheets.push(tmp)
              let tmp1 = []
              for (let i in tmp.data) {
                tmp1.push(i)
              }
            }            
          }
          that.$emit('success', that.dataSheets)
          that.loading = false
        } catch (e) {
          console.log(e)
          this.$error('分析失败！')
          return false
        }
      }
      fileReader.onloadstart = (ev) => {
        that.loading = true
        that.dataSheets = []
      }
      fileReader.readAsBinaryString(file.raw)
    },
    parseXLSX (data) {
      if (!data['!ref']) {
        return false
      }
      let ROWS = data['!ref'].replace(/[A-Za-z]+/g, '').split(":")
      let COLS = data['!ref'].replace(/[0-9]+/g, '').split(":")
      let re = []
      let res = {
        fields: [],
        rows: ROWS,
        cols: [this.ABC.indexOf(COLS[0]), this.ABC.indexOf(COLS[1])],
        data: []
      }
      for (let i = res.rows[0]; i <= res.rows[1]; i++) {
        let tmp = {}
        for (let j = res.cols[0]; j <= res.cols[1]; j++) {
          let key = this.ABC[j] + i
          if (data[key]) {
            tmp[j] = data[key]['w'] || data[key]['v']
          }
        }
        re.push(tmp)
      }
      let len = 0
      let findex = 0
      for (let i in re) {
        let newlen = Object.keys(re[i]).length
        if (newlen > len) {
          len = newlen
          findex = i
          res.cols[1] = len
        }
        if (newlen < 1) {
          delete (re[i])
        }
      }
      res.fields = re[findex]
      delete (re[findex])
      res.data = re
      return res
    },
    init () {
      let Chart = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'.split('')
      for (let i in Chart) {
        this.ABC.push(Chart[i])
      }
      for (let i in Chart) {
        if (['A', 'B'].indexOf(Chart[i]) !== -1) {
          for (let j in Chart) {
            this.ABC.push(Chart[i] + Chart[j])
          }
        }
      }
    }
  }
}
</script>
<style scoped lang="scss">
.xlsx-btns {display: inline-block;}
</style>